/*
 * PrivMsgCatcher.java
 *
 * Created on 1 Октябрь 2007 г., 16:14
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package xmppbot;

import java.sql.SQLException;
import java.util.Date;
import java.util.GregorianCalendar;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.muc.MultiUserChat;
import xmppbot.parse.options;
import xmppbot.parse.utils;

/**
 *
 * @author lian
 */
public class privMsgCatcher implements PacketListener
{
    String Room;
    MultiUserChat mchat;
    XMPPConnection connect;
    boolean globlist;
    
    /** Creates a new instance of PrivMsgCatcher */
    public privMsgCatcher (String Room, XMPPConnection connect, MultiUserChat mchat, boolean flag)
    {
        this.Room = Room;
        this.mchat = mchat;
        this.connect = connect;
        this.globlist = flag;
    }

    public void processPacket (Packet packet)
    {
        // Filter parent ro om messages 
        if (StringUtils.parseBareAddress (((Message) packet).getFrom ()).equals (Room))
        {
            Date date = (Date) new GregorianCalendar().getTime().clone();
            String MsgType = "private";
            String Msg = ((Message) packet).getBody();
            String From = packet.getFrom();
            String ShortFrom = StringUtils.parseResource(packet.getFrom());
            options opts = new options(date, MsgType, Msg, From, ShortFrom, packet, connect, mchat);
            try {
                    utils.creatPacket(opts);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                } catch (XMPPException ex1) {
                    ex1.printStackTrace();
                }
        }
        else
        {
            boolean flag = false;
            for (int i = 0; i < Main.Connect.mchats.size(); i++) {
                flag = (StringUtils.parseBareAddress((String) Main.Connect.mchatsnames.get(i))).equals(StringUtils.parseBareAddress(packet.getFrom()));
            }
            if (((Message) packet).getType() != Message.Type.groupchat && !flag && globlist) {
                Date date = ((Date) new GregorianCalendar().getTime().clone());
                String From = packet.getFrom ();
                String ShortFrom = StringUtils.parseName(packet.getFrom());
                String MsgType = "chat";
                String Msg = ((Message) packet).getBody();
                options opts = new options(date, MsgType, Msg, From, ShortFrom, packet, connect, mchat);
                try {
                    utils.creatPacket(opts);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                } catch (XMPPException ex1) {
                    ex1.printStackTrace();
                }
            }
        }
    }
    
}
